<?php
class KehoachbhldsController extends AppController {

	var $name = 'Kehoachbhlds';
	var $uses = array('Kehoachbhld','Chitieunv','Hanghoabhld');

	function index() {
		$this->Kehoachbhld->recursive = 0;
		$this->set('kehoachbhlds', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid kehoachbhld', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('kehoachbhld', $this->Kehoachbhld->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Kehoachbhld->create();
			if ($this->Kehoachbhld->save($this->data)) {
				$this->Session->setFlash(__('The kehoachbhld has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The kehoachbhld could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid kehoachbhld', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Kehoachbhld->save($this->data)) {
				$this->Session->setFlash(__('The kehoachbhld has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The kehoachbhld could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Kehoachbhld->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for kehoachbhld', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Kehoachbhld->delete($id)) {
			$this->Session->setFlash(__('Kehoachbhld deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Kehoachbhld was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function hienkehoach($nhanvien_id = null)
	{
		/*
		$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$now,
																		'Kehoach.ngaykt >='=>$now,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <='=>7),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];
		$ngaybd = $tg['Kehoach']['ngaybd'];
		$ngaykt  = $tg['Kehoach']['ngaykt'];*/
		
		$now = date('Y-m-d');
		$ngaybd = $now;
		$ngaykt  = $now;
		$this->Kehoachbhld->recursive = 1;
		$this->paginate = array('order'=>array('thoigian DESC'),
							'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt));
		$this->set('chitieungays', $this->paginate());
		/*$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt),
											'order'=>array('thoigian DESC')));*/
		/*$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh,
																'Chitieunv.nhanvien_id'=>$nhanvien_id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));*/
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		$this->set(compact('hanghoas','ngaybd','ngaykt','nhanvien_id'));
	}
	
	function laychitieu($nhanvien_id=null)
	{
		$this->layout='ajax';
		
		$ngaybd = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaybd']));
		$ngaykt  = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaykt']));
		$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$ngaybd,
																		'Kehoach.ngaykt >='=>$ngaykt,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >'=>8),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];
		$this->Kehoachbhld->recursive = 1;
		$this->paginate = array('order'=>array('thoigian DESC'),
							'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt));
		$this->set('chitieungays', $this->paginate());
		$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh,
																'Chitieunv.nhanvien_id'=>$nhanvien_id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));
		
		$this->set(compact('hanghoas','chitieungays'));
	}

	function ketquabanhang($nhanvien_id = null)
	{
		$now = date('Y-m-d');
		$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$now,
																		'Kehoach.ngaykt >='=>$now,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <='=>7),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];
		/*$ngaybd = $tg['Kehoach']['ngaybd'];
		$ngaykt  = $tg['Kehoach']['ngaykt'];*/
		$ngaybd= $now;
		$ngaykt= $now;
		$this->Kehoachbhld->recursive = 1;
		$this->paginate = array('order'=>array('thoigian DESC'),
							'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt));
		$this->set('chitieungays', $this->paginate());
		/*$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt),
											'order'=>array('thoigian DESC')));*/
		/*$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh,
																'Chitieunv.nhanvien_id'=>$nhanvien_id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));*/
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		$this->set(compact('hanghoas','ngaybd','ngaykt','nhanvien_id'));
	}
	
	function layketqua($nhanvien_id=null)
	{
		$this->layout='ajax';
		
		$ngaybd = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaybd']));
		$ngaykt  = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaykt']));
		$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$ngaybd,
																		'Kehoach.ngaykt >='=>$ngaykt,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >'=>8),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];
		$this->Kehoachbhld->recursive = 1;
		$this->paginate = array('order'=>array('thoigian DESC'),
							'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt));
		$this->set('chitieungays', $this->paginate());
		$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh,
																'Chitieunv.nhanvien_id'=>$nhanvien_id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));
		
		$this->set(compact('hanghoas','chitieungays'));
	}

	
}
